/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved. 
 */
package com.icinfo.cloud.provider.punish.usersign.service.impl;

import cn.hutool.http.HttpStatus;
import com.icinfo.cloud.common.core.config.RedisRepository;
import com.icinfo.cloud.common.security.util.UserUtil;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.common.utils.BaseUtil;
import com.icinfo.cloud.provider.common.utils.HeaderUtil;
import com.icinfo.cloud.provider.peripheralinterface.taskcenter.service.ITaskCenterService;
import com.icinfo.cloud.provider.punish.usersign.dto.MemberUserSignSaveDto;
import com.icinfo.cloud.provider.punish.usersign.mapper.MemberUserSignMapper;
import com.icinfo.cloud.provider.punish.usersign.model.MemberUserSign;
import com.icinfo.cloud.provider.punish.usersign.service.IMemberUserSignService;
import com.icinfo.cloud.xzcf.provider.member.userinfo.dto.UserDTO;
import com.icinfo.framework.common.web.Result;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述: 用户签名信息表 member_user_sign 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2022年07月21日
 */
@Slf4j
@Service
public class MemberUserSignServiceImpl extends MyBatisServiceSupport implements IMemberUserSignService {

    @Resource
    private MemberUserSignMapper memberUserSignMapper;

    @Resource
    private ITaskCenterService taskCenterService;

    @Resource
    private RedisRepository redisRepository;

    /**
     * 【V1.4.1】描述： 设置签名
     *
     * @author: zhengqiang
     * @date: 2022/7/21
     * @param: saveDto
     * @return {@link boolean}
     **/
    @Override
    public Result saveUserSign(MemberUserSignSaveDto saveDto) {
        Map<String, String> headers = new HashMap<>();
        String token = redisRepository.get("USER:TOKEN:" + saveDto.getUserId());
        if (ObjectUtils.isEmpty(token)) {
            return Result.error("未登录或登录信息已失效");
        }
        headers.put("Authorization", token);
        //调用执法平台接口设置签名
        Result result = taskCenterService.saveUserSign(saveDto.getUserSignUrl(), headers, saveDto.getUserName());
        if (!ObjectUtils.isEmpty(result) && HttpStatus.HTTP_OK == result.getCode()) {
            //设置签名成功
            MemberUserSign userSign = selectUserSign(saveDto.getUserId());
            if (ObjectUtils.isEmpty(userSign)) {
                //新增
                userSign = new MemberUserSign();
                BeanUtils.copyProperties(saveDto, userSign);
                userSign.setId(BaseUtil.createUid());
                memberUserSignMapper.insertSelective(userSign);
            } else {
                //修改
                userSign.setUserSignUrl(saveDto.getUserSignUrl());
                memberUserSignMapper.updateByPrimaryKeySelective(userSign);
            }
            //删除redis缓存token
            redisRepository.del("USER:TOKEN:" + saveDto.getUserId());


            return Result.success("设置签名成功");
        }
        return Result.error("设置签名失败");
    }

    /**
     * 【V1.3.2】描述： 查询用户签名记录
     *
     * @author: zhengqiang
     * @date: 2022/7/21
     * @param: userId
     * @return {@link String}
     **/
    @Override
    public MemberUserSign selectUserSign(String userId) {
        Example userSign = new Example(MemberUserSign.class);
        userSign.createCriteria().andEqualTo("userId", userId);
        List<MemberUserSign> memberUserSigns = memberUserSignMapper.selectByExample(userSign);
        if (!ObjectUtils.isEmpty(memberUserSigns)) {
            return memberUserSigns.get(0);
        }
        return null;
    }

    /**
     * 描述： 设置用户token
     *
     * @author: zhengqiang
     * @date: 2022/9/8
     * @return {@link Result}
     **/
    @Override
    public Result saveUserToken() {
        Map<String, String> headers = HeaderUtil.headerInfo();
        String userToken = headers.get("authorization");
        if (ObjectUtils.isEmpty(userToken)) {
            userToken = headers.get("Authorization");
        }
        if (!ObjectUtils.isEmpty(userToken)) {
            UserDTO userDTO = UserUtil.getUserInfo();
            redisRepository.setExpire("USER:TOKEN:" + userDTO.getUserId(), userToken,3600L);
            return Result.success(Constant.STRING_SUCCESS_EN);
        }
        return Result.error("未登录或登录信息已失效");
    }
}